package Cantella::DJabberd::Web::InterfaceModel::Action::User::Create;

use Reaction::Class;

use aliased 'Reaction::InterfaceModel::Action::DBIC::ResultSet::Create' =>
    'DBIC_Create';

use Reaction::Types::Core qw/NonEmptySimpleStr Password/;

class Create, is DBIC_Create, which{

  has username => (is => 'rw', isa => NonEmptySimpleStr, lazy_fail => 1 );
  has password => (is => 'rw', isa => Password, lazy_fail => 1);
  has confirm_password => (is => 'rw', isa => Password, lazy_fail => 1);

  around parameter_hashref => sub {
    my $super = shift;
    my ($self) = @_;
    my $args = $super->(@_);
    delete $args->{confirm_password};
    return $args;
  };

  around error_for_attribute => sub {
    my $super = shift;
    my ($self, $attr) = @_;
    if ($attr->name eq 'confirm_password') {
      return "New password doesn't match"
        unless $self->verify_confirm_password;
    }
    return $super->(@_);
  };

  around can_apply => sub {
    my $super = shift;
    my ($self) = @_;
    return 0 unless $self->verify_confirm_password;
    return $super->(@_);
  };

  implements verify_confirm_password => as {
    my $self = shift;
    return ($self->has_password && $self->has_confirm_password)
      && ($self->password eq $self->confirm_password);;
  };

};

1;
